var thesum=0;    //待办事项返回数量
var thesum1=0;   //已完成返回数量
var resdata=[];  //待办事项返回数据
var resdata1=[]; //已完成返回数据
var username = sessionStorage.getItem("name"); 
var now = getNowFormatDate();
document.getElementById("startDate").value=now;
document.getElementById("endDate").value=now;
$('#name').text(sessionStorage.getItem("name"));
var start=$('#startDate').val()+' 00:00:00';
var end = $('#endDate').val()+' 23:59:59';
var startDatecalendar = new lCalendar();
var endDatecalendar = new lCalendar();
var _config ;
var photoUrl = [];//拍照列表
var thecolor = 'red'//未回复 未完成的字体颜色 
$(document).ready(function(){  //界面切换

 	if(sessionStorage.getItem("Creload")=='true'){
	     window.location.reload();
		 sessionStorage.setItem("Creload", false);
	}else{

	$('#menuDiv .row_w30').on('click',function(){
		$(this).siblings().removeClass('orange').removeClass('add_bd_o');
		$(this).addClass('orange').addClass('add_bd_o');
		if($(this).attr('showType') == 'wei'){
			$('#weiDiv').show();
			$('#daibanid').show();
			$('#yiDiv').hide();
		}else{
			$('#weiDiv').hide();
			$('#daibanid').hide();
			$('#yiDiv').show();
		}
	});
  }
	
});

var sendData = {};  //待办事项
sendData.planuserid = sessionStorage.getItem("userId");
sendData.flag=10;
sendData.flag1=12;
//sendData.flag2=33;
var sendData1 = {};  //已完成
sendData1.planuserid = sessionStorage.getItem("userId");
sendData1.planstartDate = $('#startDate').val();
sendData1.planendDate = $('#endDate').val();
sendData1.flag=9;
$.ajax({              //待办事项
	url: "../../work_plan_info/getBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(sendData) 
	,dataType:'json'
	,success: function(res){
		console.log(res.data);
		for(var p in res.data){//遍历json对象的每个key/value对,p为key
 		    var  shuzu = res.data[p];
				for(var p1 in shuzu){
					var thelength = shuzu[p1].length;
					var theshuzu = shuzu[p1];
							for(var a1=0;a1<thelength;a1++){
								resdata.push(theshuzu[a1]);
							}
 				}
		}
		thesum=resdata.length;
		$('#nbbox').html(thesum);
	}
});
$.ajax({          //已完成
	url: "../../work_plan_info/getBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(sendData1) 
	,dataType:'json'
	,success: function(res){
		for(var p in res.data){//遍历json对象的每个key/value对,p为key   
 		    var  shuzu = res.data[p];
				for(var p1 in shuzu){
					var thelength = shuzu[p1].length;
					var theshuzu = shuzu[p1];
							for(var a1=0;a1<thelength;a1++){
								resdata1.push(theshuzu[a1]);
							}
 				}
		}
		thesum1=resdata1.length;
	}
});
startDatecalendar.init({
	'trigger': '#startDate',
	'type': 'date',
	'maxDate': '2099-12-31',
	'minDate': '2010-01-01'
});
endDatecalendar.init({
	'trigger': '#endDate',
	'type': 'date',
	'maxDate': '2099-12-31',
	'minDate': '2010-01-01'
});
function ICalendarClick(){
	
}
$(document).ready(function(){
	var date =new Date()
	var year=date.getFullYear();
	var month=date.getMonth()+1;
	var theday = date.getDate(); 
	if(month>=0&&month<=9){month='0'+month}
	var lastDay=new Date(year,month,0).getDate();
	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
	document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
	daiban();
	Completed();
	Searchbytime();
	$('#searchButton').on('click',Searchbytime);
	$('#custom').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="";//隐藏
	});
	$('#month').on('click',function(){
			document.getElementById("renyiriqibox").style.display="none";//隐藏
		   	document.getElementById("startDate").value=year+'-'+month+'-'+'01';
			document.getElementById("endDate").value=year+'-'+month+'-'+lastDay;
 			Searchbytime();
	});
	$('#week').on('click',function(){
			 document.getElementById("renyiriqibox").style.display="none";//隐藏
		     document.getElementById("startDate").value=getCurrentWeekFirstDay(year+'-'+month+'-'+theday);
			 document.getElementById("endDate").value=getCurrentWeekLastDay(year+'-'+month+'-'+theday);
		     Searchbytime();
	});
});	
function Searchbytime(){    //已完成的查询功能
	sendData1.planstartDate = $('#startDate').val();
	sendData1.planendDate = $('#endDate').val();
			$.ajax({
			url: "../../work_plan_info/getBody"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify(sendData1) 
			,dataType:'json'
			,success: function(res){				
				 		resdata1=[];
						for(var p in res.data){//遍历json对象的每个key/value对,p为key   
 		    				var  shuzu = res.data[p];
							for(var p1 in shuzu){
								var thelength = shuzu[p1].length;
								var theshuzu = shuzu[p1];
										for(var a1=0;a1<thelength;a1++){	
											resdata1.push(theshuzu[a1]);
										}
			 				}
						}
			thesum1=resdata1.length;
			$('#resultDiv').html("");

			}
		});
		Completed();
}	
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			photoUrl.push(result[0]);
			$('#paizhapoUrl .clearfix').remove()
			$('#paizhapoUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#paizhapoUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		photoUrl.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	console.log(photoUrl);
	console.log(imgOne);
	dd.biz.util.previewImage({
		urls: photoUrl,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
			
		},
		onFail : function(err) {}
	})
}
//获取当前日期
function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
}
function daiban(){  //待办事项
			var sumone = 0;	var sumtwo = 0;	var sumthree = 0;	var sumfour = 0;
			for(var i=0;i<thesum;i++){	
				if(resdata[i].jindu==undefined){
				resdata[i].jindu="";
				}	
			var thedisplay='';
			var thedisplay1='display:none';
			var thedisplay2='display:none';
			var theflag='未回复';
			var xieruid = '';

			var showname=''; var showsum=0;
			if(resdata[i].flag==10){thedisplay='display:none';thedisplay1='';theflag='未完成'; thecolor = 'blue'}	  //控制显示的按钮
			if(resdata[i].flag==500){theflag='已回复'; thecolor = 'blue'}	
			if(resdata[i].enddate==undefined){resdata[i].enddate=" "};     //避免显示undefined
			if(resdata[i].endtime==undefined){resdata[i].endtime=" "};     //避免显示undefined
			if(resdata[i].replydate==undefined){resdata[i].replydate=" "}; //避免显示undefined
			if(resdata[i].plantype==1){xieruid = 'gerenDiv';thedisplay2 = '';thedisplay1='display:none';thedisplay='display:none'; sumfour+=1; showname = '个人计划' ;showsum = sumfour;}
			else if(resdata[i].plantype==2&&resdata[i].flag == 12){xieruid = 'weihfDiv';sumtwo+=1; showname = '未回复事项';showsum = sumtwo;}
			else if(resdata[i].plantype==2&&resdata[i].flag == 500){xieruid = 'yihfDiv';sumthree+=1; showname = '已回复事项'; showsum = sumthree;}
			else if(resdata[i].plantype==2&&resdata[i].flag == 10){xieruid = 'weiDiv';sumone+=1; showname = '未完成事项'; showsum = sumone;}
	$('#'+xieruid).append('<div class="widget-header widget-header-flat widget-header-small" style="border-top:10px #e5e5e5 solid;">'   //待办事项标题 e5e5e5 
					+'<h5 class="black" style="">'+showname+showsum+'</h5>'
					+'<div class="widget-toolbar" id="shangpinmore">'
					+'<h5><font color="orange">'+theflag+'</font></h5>'
					+'</div>'
					+'</div>');					
					
	$('#'+xieruid).append('<div class="col_box">'                                               //待办事项内容
						+'<div>'							
						+'<div class="widget-box">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_bgrey">'
						+'<h5 id="wentishuomingTitle"><i class="iconfont">&#xe600;</i>&nbsp;工作内容</h5><span class="red"></span>'
						+'</div>'
						+'<div class="widget-body zhediebox1">'
						+'<div class="widget-main pd_div" style="padding:0;">'
						+'<div class="clearfix jy_div" style="padding:0;">'
						+'<textarea id="content'+i+'"  rows="" cols="" class="pull-left add_bor_none add_pad_tb10p" readonly="readonly" >'+resdata[i].content+'</textarea>'
						+'</div>'
						+'</div></div></div></div>'	
						
						+'<div style="'+thedisplay1+'">'	
						+'<div class="widget-box">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_bgrey ">'				
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;预计完成时间<span class="red"></span></h5>'					
						+'<input type="text"  id="enddate'+i+'"  autocomplete="off"   class="pull-right  add_bor_none layui-input " style="width: 60%;margin-right: 2%;border:none" readonly="readonly" value="'+resdata[i].enddate+' '+resdata[i].endtime+'"/>'					    
						+'</div>'				
						+'</div>'			
						+'</div>'
														
						+'<div style="'+thedisplay+'">'	
						+'<div class="widget-box">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_bgrey ">'				
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;预计回复时间<span class="red"></span></h5>'					
						+'<input type="text"  id="replydate'+i+'"  autocomplete="off"   class="pull-right  add_bor_none layui-input " style="width: 60%;margin-right: 2%;border:none" readonly="readonly" value="'+resdata[i].replydate+'"/>'					    
						+'</div>'				
						+'</div>'			
						+'</div>'
								
						+'<div>'		
						+'<div class="widget-box">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_bgrey">'       			
						+'<h5><i class="iconfont">&#xe603;</i>&nbsp;完成进度<span class="red"></span></h5>'	
						+'<input type="text" id="jindu'+i+'" class="pull-right add_bor_none" autocomplete="off" style="width: 70%;margin-right: 2%;border:none" readonly="readonly" value="'+resdata[i].jindu+'" />'
						+'</div>'	
						+'</div>'
						+'</div>'	
						+'<div class="buttonbox" id="saveButton" style="margin-top:10px;height: 30px">'	

						+'<div class="submit_button w94 bg_orange" id="tofinish'+i+'" style="'+thedisplay+'">回复</div>'

						//+'<div class="submit_button bg_orange" id="toedit'+i+'" style="height:28px;line-height:28px;margin-top:-28px;margin-left:220px;">编辑</div>'	
						+'<div class="submit_button w94 bg_orange" id="genjin'+i+'" style="'+thedisplay1+'">跟进</div>'			
						+'<div class="submit_button w94 bg_orange" id="jhgenjin'+i+'" style="'+thedisplay2+'">跟进</div>'					
						+'</div>'
						+'</div>'

	);
	
	//待办事项完成按钮
	var tofinishid = "tofinish"+i;
	tofinish=document.getElementById(tofinishid);
	if(resdata[i].replycontent!=''&&resdata[i].replycontent!=null){
		tofinish.onclick=function(){
		    var id = $(this).attr('id');
		    var num = id.substring(8);  
	 		url = "work_plan_finish?pno="+resdata[num].pno;//此处拼接内容
	        window.location.href = url;
	    }
	}else{
		tofinish.onclick=function(){
		    var id = $(this).attr('id');
		    var num = id.substring(8);  
	 		url = "work_plan_finish?pno="+resdata[num].pno;//此处拼接内容
	        window.location.href = url;
	    }
	}
	//待办事项跟进按钮
	var genjinid = "genjin"+i;
	genjin=document.getElementById(genjinid);
	genjin.onclick=function(){
 		var id = $(this).attr('id');
	    var num = id.substring(6); 
 	    url = "work_plan_genjin?pno="+resdata[num].pno;//此处拼接内容
        window.location.href = url;
    }
	//个人计划跟进按钮
	var jhgenjinid = "jhgenjin"+i;
	jhgenjin=document.getElementById(jhgenjinid);
	jhgenjin.onclick=function(){
 		var id = $(this).attr('id');
	    var num = id.substring(8); 
 	    url = "work_plan_JHgenjin?pno="+resdata[num].pno;//此处拼接内容
        window.location.href = url;
    }

    thecolor = 'red';  
}         //设置各项总数
		if(sumone!=0){$('#sumOne').show();} 
		if(sumtwo!=0){$('#sumTwo').show();}
		if(sumthree!=0){$('#sumThree').show();} 
		if(sumfour!=0){$('#sumFour').show();}
		$('#sumOne').html(sumone);
		$('#sumTwo').html(sumtwo);
		$('#sumThree').html(sumthree);
		$('#sumFour').html(sumfour);        
}
function Completed(){   //已完成	
	for(var i=0;i<thesum1;i++){
    var plantitle = '';
    if(resdata1[i].plantype=='2'){
	     if(resdata1[i].meetingtheme!=undefined)
	     plantitle = resdata1[i].meetingtheme + ' 月工作内容';
         else{plantitle = '';}
	}else{
		plantitle =resdata1[i].planuser + '的日工作内容'
	}
	$('#resultDiv').append('<div class="widget-header widget-header-flat widget-header-small">'
																		+'<h5 style="font-weight:bold;color:#eb6100;">'+plantitle+'</h5>'
																		+'<div class="widget-toolbar" id="shangpinmore">'
																		+'<span>'+resdata1[i].plandate+'</span>'
																		+'</div>'
																		+'</div>');
																
	if(resdata1[i].content==undefined){resdata1[i].content="";}
	if(resdata1[i].enddate==undefined){resdata1[i].enddate="";}
	if(resdata1[i].completedate==undefined){resdata1[i].completedate="";}
	if(resdata1[i].replycontent==undefined){resdata1[i].replycontent="";}
	if(resdata1[i].completedate==undefined){resdata1[i].completedate="";}
	$('#resultDiv').append('<div class="col_box">'
						+'<div>'
						+'<div class="widget-box add_mar_tb7p">'
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5><i class="iconfont">&#xe603;</i>&nbsp;工作内容</h5>'
						+'<input type="text" readonly="readonly" id="content1'+i+'" autocomplete="off"   class="pull-right add_bor_none" style="width: 60%;margin-right: 2%;border:none" value="'+resdata1[i].content+'"/>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'<div>'
						+'<div class="widget-box add_mar_tb7p">'
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;预计完成时间</h5>'
						+'<input type="text"  readonly="readonly" id="enddate1'+i+'"  autocomplete="off"   class="pull-right  layui-input add_bor_none" style="width: 60%;margin-right: 2%;border:none" value="'+resdata1[i].enddate+'"/>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'<div>'
						+'<div class="widget-box add_mar_tb7p">'
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;实际完成时间</h5>'
						+'<input type="text" readonly="readonly" id="completedate1'+i+'" class="pull-right add_bor_none" autocomplete="off" style="width: 60%;margin-right: 2%;border:none" value="'+resdata1[i].completedate+'"/>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'<div>'
						+'<div class="widget-box add_mar_tb7p">'
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5><i class="iconfont">&#xe600;</i>&nbsp;结果说明</h5>'
						+'<input type="text" readonly="readonly" id="replycontent1'+i+'" class="pull-right add_bor_none" autocomplete="off" style="width: 60%;margin-right: 2%;border:none" value="'+resdata1[i].replycontent+'"/>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'<div>'
						+'<div class="widget-box">'
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5><i class="iconfont">&#xe600;</i>&nbsp;附件</h5>'
						+'</div>'
						+'<div class="widget-body">'
						+'<div class="widget-main" id="paizhapoUrl">'
						+'<div class="wig_img">'
						+'<div class="img_box" id="img_box'+i+'">'
						+'</div>'
						+'</div>'
						+'<div class="clearfix"></div>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'</div>'
						+'</div>'
	);
	       //已完成 图片显示
			if(resdata1[i].img==undefined||resdata1[i].img==""){
                     console.log("null");
                }else{
					photoUrl=[];
					photoUrl.push(resdata1[i].img);	
					var result=resdata1[i].img.split(",");	
					for(var ii=0;ii<result.length;ii++){
					$('#img_box'+i).append('<img alt="100x100" src="'+result[ii]+'" onclick="showAllPic(\''+result[ii]+'\')"/>');
					}
				}
}
}
//将返回数据按照某个字段从小到大排序
/*	for (j = 0; j < resdata.length - 1; j++){
			for (i = 0; i < resdata.length - 1 - j; i++)
			if (resdata[i].plandate > resdata[i + 1].plandate)
			{
			temp =resdata[i];
			resdata[i] = resdata[i + 1];
			resdata[i + 1] = temp;
		   }
	}	*/	
